<template>
	<view class="safety homePage ">

		<view class=" headerBox home-top" :style="{paddingTop: `${systemBarHeight+10}px`}">

			<view class="search-box flex align">
				<image src="https://wx.2024csnl.com/static/home/home-search.png" mode="aspectFill" class="home-search" />
				<input type="text" class="home-search-input" placeholder="搜索关键词" v-model="keyWords" @confirm="onSearch">
				<view class="home-search-btn" @click="onSearch">搜索</view>
			</view>
		</view>
		<view class="mainBox" :style="{paddingTop:`${systemBarHeight+50}px`}">
		</view>
		<scroll-view scroll-x="true" class="scroll-box flex align">
			<view v-for="(nav,i) in navList" :key="i" :class="navIndex==nav.id?'nav-act':''" class="nav-item"
				@click="checkNav(nav.id)">{{ nav.title }}
			</view>
		</scroll-view>
		<!-- 淘宝 -->
		<view class="goods-list between" v-if="navIndex==1">
			<view class="goods-item column" v-for="(tb,index) in list" :key="index"
				@click="onSkip(`/business/pages/business_tab/thirdPartyDetails?goodsId=${tb.goodsId}&pagetype=TB`)">
				<image :src="tb.mainPic" mode="aspectFill" class="goods-img" />
				<view class="goods-info">
					<view class="">{{ tb.title }}</view>
					<!-- 优惠券 -->
					<view class="flex" style="align-items: center;flex-wrap: wrap;">
						<view class="flex leftBigBtn">
							<view class="left">可享补贴</view>
							<view class="right">￥{{ tb.commission }}</view>
						</view>
						<view class="ticketBox flex-center" v-if="tb.couponPrice">{{ tb.couponPrice }}元券</view>
					</view>
					<view class="flex">
						<view class="goods-price">
							<text>￥</text>
							<text class="price">{{ tb.actualPrice }}</text>
						</view>
						<view class="goods-price grayPrise">
							<text>￥</text>
							<text class="price">{{ tb.originalPrice }}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 京东 -->
		<view class="goods-list between" v-else-if="navIndex == 2 ">
			<view class="goods-item column" v-for="(jd,index) in list" :key="index"
				@click="onSkip(`/business/pages/business_tab/thirdPartyDetails?goodsId=${jd.skuId}&pagetype=JD`)">
				<image :src="jd.whiteImage" mode="aspectFill" class="goods-img" />
				<view class="goods-info">
					<view class="">{{ jd.skuName }}</view>
					<!-- 优惠券 -->
					<view class="flex" style="align-items: center;flex-wrap: wrap;">
						<view class="flex leftBigBtn">
							<view class="left">可享补贴</view>
							<view class="right">￥{{ jd.commission }}</view>
						</view>
						<view v-if="jd.discount" class="ticketBox flex-center">{{ jd.discount }}元券</view>
					</view>
					<view class="flex">
						<view class="goods-price">
							<text>￥</text>
							<text class="price">{{ jd.price }}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 拼多多 -->
		<view class="goods-list between" v-else-if="navIndex == 4 ">
			<view class="goods-item column" v-for="(pdd,index) in list" :key="index"
				@click="onSkip(`/business/pages/business_tab/thirdPartyDetails?goodsId=${pdd.goodsSign}&pagetype=PDD`)">
				<image :src="pdd.goodsImageUrl" mode="aspectFill" class="goods-img" />
				<view class="goods-info">
					<view class="">{{ pdd.goodsName }}</view>
					<!-- 优惠券 -->
					<view class="flex" style="align-items: center;flex-wrap: wrap;">
						<view class="flex leftBigBtn">
							<view class="left">可享补贴</view>
							<view class="right">￥{{ pdd.commission }}</view>
						</view>
						<view v-if="pdd.couponDiscount" class="ticketBox flex-center">
							{{ pdd.couponDiscountcouponDiscount }}元券
						</view>
					</view>
					<view class="flex">
						<view class="goods-price">
							<text>拼团￥</text>
							<text class="price">{{ pdd.minGroupPrice }}</text>
						</view>
						<view class="goods-price grayPrise">
							<text>￥</text>
							<text class="price">{{ pdd.minNormalPrice }}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="flex-center addressImgBox" v-if="list.length==0">
			<image src="https://wx.2024csnl.com/static/noData.png"></image>
			暂未找到~
		</view>
		<view style="text-align: center;color: #999;" v-if="noMoreData&&list.length!=0">没有更多啦~</view>

	</view>
</template>

<script>
	import {
		getTaoBaoSearch,
		getJDList,
		getWeiPinHuiSearch,
		getPDDList
	} from "../api/index.js"

	export default {
		data() {
			return {
				pageSize: 20,
				pagenum: 1,
				totalPage: '',
				noMoreData: false,
				keyWords: '',
				navList: [{
						title: '淘宝',
						id: 1
					},
					{
						title: '京东',
						id: 2
					},
					{
						title: '唯品会',
						id: 3
					},
					{
						title: '拼多多',
						id: 4
					},
				],
				navIndex: 1,
				systemBarHeight: getApp().globalData.systemBarHeight,
				list: [],
			}
		},
		onLoad() {
			console.log(this.navList, "222")
		},
		onReachBottom() {
			if (this.pagenum < this.totalPage) {
				this.pagenum += 1
				switch (true) {
					case this.navIndex == 1:
						this.onGetTaoBaoSearch('bottom')
						break;
					case this.navIndex == 2:
						this.onGetJDList('bottom')
						break;
					case this.navIndex == 3:
						this.onGetWPHList('bottom')
						break;
					case this.navIndex == 4:
						this.onGetPDDList('bottom')
						break;
				}
			} else {
				this.noMoreData = true
			}

		},
		methods: {
			onSearch() {
				if (this.keyWords == '') {
					this.showToast('请输入搜索内容')
					return
				}
				this.list = []
				this.pagenum = 1
				switch (true) {
					case this.navIndex == 1:
						this.onGetTaoBaoSearch()
						break;
					case this.navIndex == 2:
						this.onGetJDList()
						break;
					case this.navIndex == 3:
						this.onGetWPHList()
						break;
					case this.navIndex == 3:
						this.onGetWPHList()
						break;
					case this.navIndex == 4:
						this.onGetPDDList()
						break;
				}
			},
			// 拼多多搜索列表
			async onGetPDDList(type) {
				const res = await getPDDList('s=/ApiDataokeGoods/pddsearch', {
					// catId: this.navIndex,
					withCoupon: 1, //是否只返回优惠券商品
					page: this.pagenum,
					keyword: this.keyWords
				})
				console.log(res, "res---")
				const {
					status,
					msg,
					data
				} = res.data
				if (status == 1) {
					this.totalPage = data.totalCount
					type == 'bottom' ? this.list.push(...data.goodsList) : this.list = data.goodsList
					this.pagenum != 1 && data.goodsList.length == 0 ? this.noMoreData = true : this.noMoreData = false
					console.log(this.list, 'this.list')
				} else {
					this.showToast(msg)
				}
			},
			// 唯品会搜索-未对接
			async onGetWPHList(type) {
				const res = await getWeiPinHuiSearch('s=/ApiDataokeGoods/wphsearch', {
					page: this.pagenum,
					keyword: this.keyWords
				})
				console.log(res, "res---")
				const {
					status,
					msg,
					data
				} = res.data
				if (status == 1) {
					// this.totalPage = data.totalNum
					// type == 'bottom' ? this.list.push(...data.list) : this.list = data.list
					// this.pagenum != 1 && data.length == 0 ? this.noMoreData = true : this.noMoreData = false
				} else {
					this.showToast(msg)
				}
			},

			// 京东搜索
			async onGetJDList(type) {
				const res = await getJDList('s=/ApiDataokeGoods/jdsearch', {
					pageSize: 20,
					pageId: this.pagenum,
					keyword: this.keyWords
				})
				console.log(res, "res---")
				const {
					status,
					msg,
					data
				} = res.data
				if (status == 1) {
					this.totalPage = data.totalNum
					type == 'bottom' ? this.list.push(...data.list) : this.list = data.list
					this.pagenum != 1 && data.length == 0 ? this.noMoreData = true : this.noMoreData = false
				} else {
					this.showToast(msg)
				}
			},
			// 淘宝搜索
			async onGetTaoBaoSearch(type) {
				const res = await getTaoBaoSearch('s=/ApiDataokeGoods/tbsearch', {
					keyWords: this.keyWords,
					pageSize: this.pageSize,
					pageId: this.pagenum
				})
				// console.log(res, "tt")
				const {
					msg,
					status,
					data
				} = res.data
				if (status == 1) {
					this.totalPage = Math.ceil(data.totalNum / this.pageSize)
					type == 'bottom' ? this.list.push(...data.list) : this.list = data.list
					this.pagenum != 1 && data.list.length == 0 ? this.noMoreData = true : this.noMoreData = false

				} else {
					this.showToast(msg)
				}
			},
			checkNav(id) {
				this.navIndex = id
				this.onSearch()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.leftBigBtn {
		color: rgb(231, 204, 134);
		font-size: 18rpx;
		margin: 15rpx 15rpx 15rpx 0;


		.left {
			padding: 10rpx;
			background: rgb(50, 60, 69);
			border-radius: 15rpx 0 0 15rpx;
		}

		.right {
			padding: 10rpx;
			border-radius: 0 15rpx 15rpx 0;
			color: #fff;
			background: rgb(240, 121, 5);
		}
	}

	.ticketBox {
		border: 1px solid rgb(198, 156, 78);
		color: rgb(198, 156, 78);
		padding: 5rpx 10rpx;
		border-radius: 15rpx;
		font-size: 18rpx;
		height: 40rpx;

	}

	.addressImgBox {
		flex-direction: column;
		color: #999;
		padding-top: 20%;

		image {
			width: 400rpx;
			height: 400rpx;
		}
	}

	.scroll-box {
		width: 710rpx;
		margin: 30rpx;
		white-space: nowrap;
		font-size: 27rpx;
		color: #B3B3B3;

		.nav-item {
			display: inline-block;
			margin-right: 32rpx;
			min-width: 20%;
		}

		.nav-act {
			color: #41644A;
		}

		.nav-item:last-child {
			margin-right: 0;
		}
	}

	.goods-list {
		padding: 20rpx;
		flex-wrap: wrap;

		.goods-item {
			width: 343rpx;
			border-radius: 20rpx;
			margin-bottom: 24rpx;
			box-shadow: 0 1rpx 10rpx 0 rgba(0, 0, 0, 0.1);

			.goods-img {
				width: 343rpx;
				height: 400rpx;
				border-radius: 20rpx 20rpx 0 0;
				background-color: #B3B3B3;
			}

			.goods-info {
				padding: 20rpx;
				font-size: 24rpx;
			}

			.goods-price {
				color: #F71212;
				font-size: 27rpx;
				margin-top: 16rpx;

				.price {
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.grayPrise {
				margin-left: 10rpx;
				color: #999;

				.price {
					font-size: 28rpx;
					font-weight: 400;
					text-decoration: line-through
				}
			}
		}

	}

	.homePage {
		.headerBox {
			background: #fff;
			// #ifdef APP-PLUS
			padding: 30rpx;
			// #endif
			// #ifdef H5
			padding: 30rpx;
			// #endif
			position: fixed;
			left: 0;
			right: 0;
			top: 0;
			z-index: 999;
		}
	}

	.search-box {
		// margin: 0 30rpx;
		background-color: #F7F7F7;
		border-radius: 13rpx;
		flex: 1;
		padding: 20rpx 28rpx;

		.home-search {
			width: 25rpx;
			height: 25rpx;
		}

		.home-search-input {
			flex: 1;
			font-size: 27rpx;
			margin-left: 16rpx;
		}

		.home-search-btn {
			padding-left: 28rpx;
			color: #41644A;
			font-size: 27rpx;
			border-left: 1rpx solid #F0F0F0;
		}
	}
</style>